package com.wanchuang.shengfubao.module.allow;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.wanchuang.shengfubao.R;
import com.wanchuang.shengfubao.base.BaseActivity;
import com.wanchuang.shengfubao.net.Apis;
import com.wanchuang.shengfubao.net.NetworkManager;
import com.wanchuang.shengfubao.net.interfaces.OnRequestCallBack;
import com.wanchuang.shengfubao.proto.Mystock;
import com.google.android.material.tabs.TabLayout;
import com.google.protobuf.InvalidProtocolBufferException;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class AllowApplyActivity extends BaseActivity {

    Unbinder unbinder;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.view_pager)
    ViewPager viewPager;
    @BindView(R.id.back_iv)
    ImageView backIv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.tv_message)
    TextView tvMessage;


    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_allow_apply);
        unbinder = ButterKnife.bind(this);
        tvTitle.setText("划拨申请");
        tvTitleRight.setText("审核记录");
        tvMessage.setMovementMethod(new ScrollingMovementMethod());
        setMessage();
        initView();
        initListener();
    }

    private void initListener() {
        backIv.setOnClickListener(view -> finish());
        tvTitleRight.setOnClickListener(v -> {
            startActivity(AllowRecordActivity.class);
        });

    }

    protected void initView() {
        String[] titles = {"出库申请", "入库申请"};
        fragmentList = new ArrayList<>();
        fragmentList.add(AllowApplyFragment.NewInstance(0));
        fragmentList.add(AllowApplyFragment.NewInstance(1));
        viewPager.setOffscreenPageLimit(2);
        viewPager.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }

            @Override
            public int getCount() {
                return fragmentList.size();
            }
            @Override
            public CharSequence getPageTitle(int position) {
                return titles[position];
            }

        });
        //设置图标
        tabLayout.setupWithViewPager(viewPager);


        for (int i = 0; i < titles.length; i++) {
            tabLayout.getTabAt(i).setCustomView(getTabView(titles[i]));
        }
    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    public void setMessage() {
        Mystock.get_sn_transfer_list.Builder builder = Mystock.get_sn_transfer_list.newBuilder();
        builder.setState("1");
        NetworkManager.INSTANCE.post(Apis.get_sn_transfer_list, builder.build().toByteArray(), 1, new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) throws InvalidProtocolBufferException {
                Mystock.get_sn_transfer_list datas = Mystock.get_sn_transfer_list.parseFrom(response);
                List<Mystock.get_sn_transfer_list.Describe> list = datas.getDesListList();
                StringBuilder des = new StringBuilder();
                for (int i = 0; i < list.size() - 1; i++) {
                    des.append(list.get(i).getDes()).append("\n");
                }
                des.append(list.get(list.size() - 1).getDes());
                tvMessage.setText(des.toString());
            }

            @Override
            public void onError(int errorCode, String errorMessage) {
                showToast(errorMessage);
            }
        });
    }




    public View getTabView(String tabName) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.tab_select_layout, null);
        TextView tv_tab = view.findViewById(R.id.tv_tab);
        tv_tab.setText(tabName);
        return view;
    }



}
